package com.wonders.bigdata.nio;

import java.nio.ByteBuffer;

/**
 * Description: <br>
 *
 * @author: XB
 * @date: 2021/5/8 14:13
 */
public class NIOByteBufferPutGet {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(65);
        buffer.putChar('万');
        buffer.putInt(10);
        buffer.putFloat(10.34f);
        buffer.putLong(100L);
        buffer.putShort((short)5);

        //反转
        buffer.flip();

        //取出,一定要按put的顺序取出
        System.out.println(buffer.getChar());
        System.out.println(buffer.getInt());
        System.out.println(buffer.getFloat());
        System.out.println(buffer.getLong());
        System.out.println(buffer.getShort());

        System.out.println("通过只读buffer获取数据");
        buffer.flip();
        buffer.putLong(100L);
        buffer.putShort((short)5);
        // 通过buffer获取制度buffer
        ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
        while (readOnlyBuffer.hasRemaining()) {
            System.out.println(readOnlyBuffer.get());
        }
    }
}
